﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Job.Data.Interfaces;
using Job.Logic.Common;
using Job.Entity;
using Job.Web.Framework;

namespace Job.Logic.Personal
{
    public class PersonalLogic
    {
        private readonly ICommon commonDao = DataAccess.commonDao;
        private readonly IJobPersonInfoDao infoDao = DataAccess.personInfoDao;
        private readonly IJobPersonWorkDao workDao = DataAccess.personWorkDao;
        private readonly IJobPersonEduDao eduDao = DataAccess.personEduDao;
        private readonly IJobPersonAccountDao accountDao = DataAccess.personAccountDao;
        private readonly IJobPersonTranDao tranDao = DataAccess.personTranDao;
        private readonly IJobPersonCertifyDao certifyDao = DataAccess.personCertifyDao;
        private readonly IJobPersonAdvanceDao advDao = DataAccess.personAdvanceDao;
        private readonly IJobPersonProjectDao projdao = DataAccess.personProjectDao;
        private readonly IJobPersonSkillDao skillDao = DataAccess.personSkillDao;

        #region 个人简历设置


        public JobPersonInfo GetSinglePersonBaseInfo(int personInfoId)
        {
            return commonDao.FindSinglePersonInfo(personInfoId);
        }

        public bool Insert(JobPersonInfo info)
        {
            try
            {
                infoDao.Insert(info);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool Update(JobPersonInfo info)
        {
            try
            {
                infoDao.Update(info);
                RefreshResume(info.PersonInfoId);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void RefreshResume(int resumeId)
        {
            commonDao.RefreshResume(resumeId);
        }

        public void UpdatePersonClass(int personInfoId, int personClass)
        {
            JobPersonInfo info = new JobPersonInfo();
            info.PersonInfoId = personInfoId;
            info.PersonClass = personClass;
            infoDao.UpdatePersonClass(info);
        }


        public void UpdatePersonStatus(int personInfoId, int status)
        {
            JobPersonInfo info = new JobPersonInfo();
            info.PersonInfoId = personInfoId;
            info.ResumeState = status;
            infoDao.UpdatePersonStatus(info);
        }

        #endregion

        #region 个人工作经历

        public bool InsertWork(JobPersonWork work)
        {
            try
            {
                workDao.Insert(work);
                RefreshResume(work.PersonInfoId.Value);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool UpdateWork(JobPersonWork work)
        {
            try
            {
                workDao.Update(work);
                RefreshResume(work.PersonInfoId.Value);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void DeleteWork(JobPersonWork work)
        {
            workDao.Delete(work);
            RefreshResume(work.PersonInfoId.Value);
        }
        public IList<CustomeWorkEntity> GetWorksByPersonalId(int personalId)
        {
            IList<CustomeWorkEntity> clist = new List<CustomeWorkEntity>();
            IList<JobPersonWork> list = workDao.FindByPersonInfoId(personalId);
            foreach (JobPersonWork work in list)
            {
                string toTime = ((work.EndYear.HasValue && work.EndYear == -1) || (work.EndMonth.HasValue && work.EndMonth.Value == -1)) ? "至今":work.EndYear + "-" + work.EndMonth;

                CustomeWorkEntity en = new CustomeWorkEntity()
                {
                    CompanyName = work.CompanyName,
                    PositionName = work.PositionName,
                    ID = work.PersonWorkId,
                    FromWorkTime = work.StartYear + "-" + work.StartMonth,
                    ToWorkTime = work.EndYear + "-" + work.EndMonth,
                    WorkTime = work.StartYear + "-" + work.StartMonth + "---" + toTime,
                    WorkDesc = work.WorkDesc,
                    ManageCount = work.ManageCount.HasValue == true ? work.ManageCount.Value.ToString() : "",
                    LeaveReason = work.LeaveReason,
                    Reference = work.Reference,
                    ReportTo = work.ReportTo,
                    Result = work.Result

                };
                clist.Add(en);
            }
            return clist;
        }

        public JobPersonWork GetSingleWork(int workid)
        {
            return workDao.Find(workid);
        }
        #endregion 
        
        #region 个人教育经历管理

        public IList<CustomeEduEntity> GetEdusByPersonalId(int personalId)
        {
            IList<CustomeEduEntity> clist = new List<CustomeEduEntity>();
            IList<JobPersonEdu> list = eduDao.FindByPersonInfoId(personalId);
            foreach (JobPersonEdu edu in list)
            {
                string _eduHeight = string.Empty;
                if (edu.EduHeight.HasValue)
                {
                    _eduHeight = MemoryDictionary.GetCommonDictName(edu.EduHeight.Value);
                }
                CustomeEduEntity en = new CustomeEduEntity()
                {
                    SchooleName = edu.SchooleName,
                    SpecialtyName = edu.SpecialtyName,
                    ID = edu.PersonEduId,
                    FromEduTime = edu.StartYear + "-" + edu.StartMonth,
                    ToEduTime = edu.EndYear + "-" + edu.EndMonth,
                    EduTime = edu.StartYear + "-" + edu.StartMonth + "---" + edu.EndYear + "-" + edu.EndMonth,
                    EduDesc = edu.EduDesc,
                    EduHeight = _eduHeight
                };
                clist.Add(en);
            }
            return clist;
        }

        public JobPersonEdu GetSingleEdu(int workid)
        {
            return eduDao.Find(workid);
        }

        public bool InsertEdu(JobPersonEdu edu)
        {
            try
            {
                eduDao.Insert(edu);
                RefreshResume(edu.PersonInfoId.Value);
            }
            catch(Exception ex)
            {
                Logger.WriteLog("Insert EDU" + ex.Message);
                return false;
            }
            return true;
        }

        public bool UpdateEdu(JobPersonEdu edu)
        {
            try
            {
                eduDao.Update(edu);
                RefreshResume(edu.PersonInfoId.Value);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void DeleteEdu(JobPersonEdu edu)
        {
            eduDao.Delete(edu);
            RefreshResume(edu.PersonInfoId.Value);
        }
        #endregion

        #region 个人培训经历管理

        public IList<CustomeTranEntity> GetTransByPersonalId(int personalId)
        {
            IList<CustomeTranEntity> clist = new List<CustomeTranEntity>();
            IList<JobPersonTran> list = tranDao.FindByPersonInfoId(personalId);
            foreach (JobPersonTran tran in list)
            {
                CustomeTranEntity en = new CustomeTranEntity()
                {
                    TranName = tran.TranName,
                    BookName = tran.TranCertify,
                    ID = tran.PersonTranId,
                    FromTranTime = tran.StartTime.Value.ToString(),
                    ToTranTime = tran.EndTime.Value.ToString(),
                    TranTime = tran.StartTime.Value.ToString("yyyy-MM") +  "---" + tran.EndTime.Value.ToString("yyyy-MM"),
                    TranDesc = tran.TranDesc
                };
                clist.Add(en);
            }
            return clist;
        }

        public JobPersonTran GetSingleTran(int tranid)
        {
            return tranDao.Find(tranid);
        }

        public bool ActionTran(JobPersonTran tran)
        {
            bool result = false;
            if (tran.PersonTranId > 0)
            {
                result = UpdateTran(tran);

            }
            else
            {
                result = InsertTran(tran);
            }

            RefreshResume(tran.PersonInfoId.Value);
            return result;
        }

        private bool InsertTran(JobPersonTran tran)
        {
            try
            {
                tranDao.Insert(tran);
            }
            catch
            {
                return false;
            }
            return true;
        }

        private bool UpdateTran(JobPersonTran tran)
        {
            try
            {
                tranDao.Update(tran);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void DeleteTran(JobPersonTran tran)
        {
            tranDao.Delete(tran);
            RefreshResume(tran.PersonInfoId.Value);
        }
        #endregion

        #region 个人信息中的大字段管理
        public string GetPersonDesc(int personId)
        {
            JobPersonInfo info = infoDao.FindPersonDesc(personId);
            if ( null == info || null == info.PersonDesc) return "";
            return info.PersonDesc;
        }
        public void SavePersonDesc(int personId,string content)
        {
            content = (null == content) ? "" : content;
            JobPersonInfo info = new JobPersonInfo();
            info.PersonInfoId = personId;
            info.PersonDesc = content;
            infoDao.UpdatePersonDesc(info);
            RefreshResume(personId);
        }

        public string GetPersonForeignResume(int personId)
        {
            JobPersonInfo info = infoDao.FindForeignResume(personId);
            if ( null == info || null == info.ForeignResume) return "";
            return info.ForeignResume;
        }
        public void SavePersonForeignResume(int personId, string content)
        {
            content = (null == content) ? "" : content;
            JobPersonInfo info = new JobPersonInfo();
            info.PersonInfoId = personId;
            info.ForeignResume = content;
            infoDao.UpdatePersonForeignResume(info);
        }

        public string GetPersonOtherDesc(int personId)
        {
            JobPersonInfo info = infoDao.FindOtherDesc(personId);
            if ( null == info || null == info.OtherDesc) return "";
            return info.OtherDesc;
        }
        public void SavePersonOtherDesc(int personId, string content)
        {
            content = (null == content) ? "" : content;
            JobPersonInfo info = new JobPersonInfo();
            info.PersonInfoId = personId;
            info.OtherDesc = content;
            infoDao.UpdatePersonOtherDesc(info);
            RefreshResume(personId);
        }

        public void UpdateCareerObjective(JobPersonInfo info)
        {
            commonDao.UpdateCareerObjective(info);
            RefreshResume(info.PersonInfoId);
        }

        public void UpDatePhoto(JobPersonInfo info)
        {
            commonDao.UpdatePhoto(info);
            RefreshResume(info.PersonInfoId);
        }

        public JobPersonInfo GetUserPhoto(int personInfoId)
        {
            return commonDao.GetUserPhoto(personInfoId);
        }
        #endregion

        #region 登录信息

        public JobPersonAccount Login(string loginName)
        {
            IList<JobPersonAccount> list = accountDao.FindByLoginName(loginName);
            if (null == list || list.Count == 0) return null;
            return list[0];
        }

        public IList<ResumeList> GetResumeList(int personAccountId)
        {
            return commonDao.GetResumeList(personAccountId);
        }

        public JobPersonAccount GetAccountByAccountid(int accountId)
        {
            JobPersonAccount list = accountDao.Find(accountId);
            return list;
        }

        public void UpdateLogin(int personAccountId)
        {
            JobPersonAccount account = new JobPersonAccount();
            account.PersonAccountId = personAccountId;
            DataAccess.personAccountDao.UpdateLogin(account);
        }

        public void CreateAccount(JobPersonAccount account)
        {
            DataAccess.personAccountDao.Insert(account);
        }
        public void UpdateAccount(JobPersonAccount account)
        {
            DataAccess.personAccountDao.Update(account);
        }

        #endregion

        #region 证书
        public JobPersonCertify GetCertify(int id)
        {
            return certifyDao.Find(id);
        }

        public IList<JobPersonCertify> GetCertifyList(int resumeId)
        {
            return certifyDao.FindByPersonInfoId(resumeId);
        }

        public void DeleteCertify(JobPersonCertify cer)
        {
            certifyDao.Delete(cer);
        }

        public void InsertCertify(JobPersonCertify cer)
        {
            certifyDao.Insert(cer);
        }

        public void UpdateCertify(JobPersonCertify cer)
        {
            certifyDao.Update(cer);
        }

        public IList<CustomeCertifyEntity> GetCerList(int resumeId)
        {
            IList<CustomeCertifyEntity> clist = new List<CustomeCertifyEntity>();
            IList<JobPersonCertify> list = certifyDao.FindByPersonInfoId(resumeId);
            foreach (JobPersonCertify pro in list)
            {
                string time = pro.GetYear.HasValue == true ? pro.GetYear.Value.ToString() : "";
                string month = pro.GetMonth.HasValue == true ? pro.GetMonth.Value.ToString() : "";
                string name = "";
                if (pro.CertifyName != null && pro.CertifyName.Length > 0)
                {
                    name = MemoryDictionary.GetFullCertificateName(pro.CertifyName);
                }
                else
                {
                    name = pro.Other;
                }
                CustomeCertifyEntity en = new CustomeCertifyEntity()
                {

                    CerName = name,
                    CerTime = time + "-" + month,
                    Result = pro.Result
                };
                clist.Add(en);
            }
            return clist;
        }
        #endregion

        #region 高级人才
        public JobPersonAdvance GetAdvance(int resumeId)
        {
            IList<JobPersonAdvance> adv = new List<JobPersonAdvance>();
            adv = advDao.FindByPersonInfoId(resumeId);
            if (adv != null && adv.Count > 0)
            {
                return adv[0];
            }
            return new JobPersonAdvance();
        }

        public IList<JobPersonAdvance> GetAdvanceList(int resumeId)
        {
            return advDao.FindByPersonInfoId(resumeId);
        }

        public void DeleteAdvance(JobPersonAdvance adv)
        {
            advDao.Delete(adv);
        }

        public void InsertAdvance(JobPersonAdvance adv)
        {
            advDao.Insert(adv);
        }

        public void UpdateAdvance(JobPersonAdvance adv)
        {
            advDao.Update(adv);
        }
        #endregion


        #region 技能水平
        public JobPersonSkill GetSkill(int id)
        {
            return skillDao.Find(id);
        }

        public IList<JobPersonSkill> GetSkillList(int resumeId)
        {
            return skillDao.FindByPersonInfoId(resumeId);
        }

        public void DeleteSkill(JobPersonSkill skill)
        {
            skillDao.Delete(skill);
        }

        public void InsertSkill(JobPersonSkill skill)
        {
            skillDao.Insert(skill);
        }

        public void UpdateSkill(JobPersonSkill skill)
        {
            skillDao.Update(skill);
        }
        #endregion


        #region 项目经验
        public JobPersonProject GetProject(int id)
        {
            return projdao.Find(id);
        }

        public IList<CustomeProjectEntity> GetProjectList(int resumeId)
        {
            IList<CustomeProjectEntity> clist = new List<CustomeProjectEntity>();
            IList<JobPersonProject> list = projdao.FindByPersonInfoId(resumeId);
            foreach (JobPersonProject pro in list)
            {
                CustomeProjectEntity en = new CustomeProjectEntity()
                {
                    ProjectName = pro.ProjectName,
                    ID = pro.PersonProjectId,
                    FromWorkTime = pro.StartYear + "-" + pro.StartMonth,
                    ToWorkTime = pro.EndYear + "-" + pro.EndMonth,
                    ProjectTime = pro.StartYear + "-" + pro.StartMonth + "---" + pro.EndYear + "-" + pro.EndMonth,
                    DevTool = pro.DevTool,
                    HardEnvironment = pro.HardEnvironment,
                    SoftEnvironment = pro.SoftEnvironment,
                    ProjectDesc = pro.ProjectDesc,
                    ResponseDesc = pro.ResponseDesc

                };
                clist.Add(en);
            }
            return clist;
        }

        public IList<CustomeProjectEntity> GetProjectListByShow(int resumeId)
        {
            IList<CustomeProjectEntity> clist = new List<CustomeProjectEntity>();
            IList<JobPersonProject> list = projdao.FindByPersonInfoId(resumeId);
            foreach (JobPersonProject pro in list)
            {
                if (pro.ShowInResume.HasValue == true && pro.ShowInResume.Value == true)
                {
                    CustomeProjectEntity en = new CustomeProjectEntity()
                    {
                        ProjectName = pro.ProjectName,
                        ID = pro.PersonProjectId,
                        FromWorkTime = pro.StartYear + "-" + pro.StartMonth,
                        ToWorkTime = pro.EndYear + "-" + pro.EndMonth,
                        ProjectTime = pro.StartYear + "-" + pro.StartMonth + "---" + pro.EndYear + "-" + pro.EndMonth,
                        DevTool = pro.DevTool,
                        HardEnvironment = pro.HardEnvironment,
                        SoftEnvironment = pro.SoftEnvironment,
                        ProjectDesc = pro.ProjectDesc,
                        ResponseDesc = pro.ResponseDesc

                    };
                    clist.Add(en);
                }
            }
            return clist;
        }

        public void DeleteProject(JobPersonProject proj)
        {
            projdao.Delete(proj);
        }

        public void InsertProject(JobPersonProject proj)
        {
            projdao.Insert(proj);
        }

        public void UpdateProject(JobPersonProject proj)
        {
            projdao.Update(proj);
        }
        #endregion

        public string GetUserName(string name)
        {
            
            IList<JobPersonAccount> list = accountDao.FindByLoginName(name);
            if (list != null && list.Count > 0)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public void EditPassword(JobPersonAccount account)
        {
            commonDao.EditPassword(account);
        }

        public JobPersonAccount GetAccountByEmail(string email)
        {
            IList<JobPersonAccount> list = accountDao.FindByLinkEmail(email);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return new JobPersonAccount();
            }
        }
    }
}
